var mongoose=require('mongoose');
var User=mongoose.model('User',{
    name:String,
    number:Number,
    winner:Boolean,
    y:Number,
    reg:Object
});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');


User.update({name:'A'},{number:1101,winner:true,reg:{val:/\w+/}},{upsert:true},(e,r)=>{
    if(e)throw e;
    console.log('Update:\n',r);

    User.update({name:'B'},{number:1201,winner:false,reg:{val:'word'}},{upsert:true},(e,r)=>{
        if(e)throw e;
        console.log('Update:\n',r);


        User.update({name:'C'},{number:1301,winner:true},{upsert:true},(e,r)=>{
            if(e)throw e;
            console.log('Update:\n',r);

            User.update({name:'D'},{number:1401,winner:false},{upsert:true},(e,r)=>{
                if(e)throw e;
                console.log('Update:\n',r);

                User.update({name:'E'},{number:1501,winner:false},{upsert:true},(e,r)=>{
                    if(e)throw e;
                    console.log('Update:\n',r);

                    User.find((e,r)=>{
                        if(e)throw e;
                        console.log('\nFind All:\n',r);

                        User.find({
                            'reg.val':/\w+/
                        },(e,r)=>{
                            if(e)throw e;
                            console.log('\nFind:\n',r);
                            process.exit(1);
                        });

                    });

                });

            });

        });

    });

});


/*
 * {field:{$exists:true}}
 *
 *
 * 字段是否有任何值（即被赋值过）
 * */
